//
//  JKRFeedCommentCountLayout.m
//  JKRFeedDemo
//
//  Created by Lucky on 2017/12/9.
//  Copyright © 2017年 Lucky. All rights reserved.
//

#import "JKRFeedCommentCountLayout.h"

@implementation JKRFeedCommentCountLayout

- (instancetype)initWithModel:(JKRFeedCommentListModel *)model {
    self = [super init];
    self.model = model;
    [self _layout];
    return self;
}

- (void)_layout {
    if (self.model.vote_users.count > 0) {
        YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
        container.maximumNumberOfRows = 1;
        NSMutableAttributedString *voteNumString = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" %zd人点赞", self.model.vote_num] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFeedCommentVoteNumFontSize], NSForegroundColorAttributeName:kFeedCommentVoteNumColor}];
        YYTextHighlight *highLight = [YYTextHighlight new];
        highLight.userInfo = @{kFeedCommentVoteTapUser:kFeedCommentVoteTapMoreUserID};
        [voteNumString setTextHighlight:highLight range:voteNumString.string.rangeOfAll];
        self.voteNumLabelLayout = [YYTextLayout layoutWithContainer:container text:voteNumString];
        self.voteNumLabelFrame = CGRectMake(kScreenWidth - 15 - self.voteNumLabelLayout.textBoundingRect.size.width, (kFeedCommentCountViewHeight - self.voteNumLabelLayout.textBoundingRect.size.height) * 0.5 + 1, self.voteNumLabelLayout.textBoundingRect.size.width, self.voteNumLabelLayout.textBoundingRect.size.height);
        
        NSMutableAttributedString *voteUserString = [NSMutableAttributedString new];
        for (JKRFeedVoteUserModel *userModel in self.model.vote_users) {
            NSAttributedString *pic = [NSAttributedString circleHeader_attributedStringWithFontSize:20 imageURL:userModel.thumbnail shrink:NO];
            YYTextHighlight *highLight = [YYTextHighlight new];
            highLight.userInfo = @{kFeedCommentVoteTapUser:userModel.uid};
            NSMutableAttributedString *picM = [[NSMutableAttributedString alloc] initWithAttributedString:pic];
            [picM setTextHighlight:highLight range:picM.string.rangeOfAll];
            [voteUserString appendAttributedString:picM];
            [voteUserString appendAttributedString:[[NSMutableAttributedString alloc] initWithString:@" "]];
        }
        if (self.model.vote_num > 3) {
            NSAttributedString *pic = [NSAttributedString original_attributedStringWithFontSize:20 image:[JKRFeedDetailHelper imageNamed:@"more_fxsy"] shrink:NO];
            [voteUserString appendAttributedString:pic];
            YYTextHighlight *highLight = [YYTextHighlight new];
            highLight.userInfo = @{kFeedCommentVoteTapUser:kFeedCommentVoteTapMoreUserID};
            [voteUserString setTextHighlight:highLight range:NSMakeRange(voteUserString.length - pic.length, pic.length)];
        }
        self.voteUserLabelLayout = [YYTextLayout layoutWithContainer:container text:voteUserString];
        self.voteUserLabelFrame = CGRectMake(kScreenWidth - self.voteUserLabelLayout.textBoundingRect.size.width - self.voteNumLabelLayout.textBoundingRect.size.width - 15, (kFeedCommentCountViewHeight - self.voteUserLabelLayout.textBoundingRect.size.height) * 0.5, self.voteUserLabelLayout.textBoundingRect.size.width, self.voteUserLabelLayout.textBoundingRect.size.height);
    } else {
        self.voteUserLabelLayout = nil;
        self.voteUserLabelFrame = CGRectZero;
        self.voteNumLabelLayout = nil;
        self.voteNumLabelFrame = CGRectZero;
    }
    {
        NSMutableAttributedString *commentCountString = [NSMutableAttributedString new];
        NSMutableAttributedString *header = [[NSMutableAttributedString alloc] initWithString:@"评价 " attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFeedCommentNumHeaderFontSize], NSForegroundColorAttributeName:kFeedCommentNumHeaderColor}];
        [commentCountString appendAttributedString:header];
        NSMutableAttributedString *number = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"(%zd条)", self.model.comment_num] attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:kFeedCommentNumFontSize], NSForegroundColorAttributeName:kFeedCommentNumColor}];
        [commentCountString appendAttributedString:number];
        YYTextContainer *container = [YYTextContainer containerWithSize:CGSizeMake(MAXFLOAT, MAXFLOAT)];
        container.maximumNumberOfRows = 1;
        self.commentCountLabelLayout = [YYTextLayout layoutWithContainer:container text:commentCountString];
        self.commentCountLabelFrame = CGRectMake(15, (kFeedCommentCountViewHeight - self.commentCountLabelLayout.textBoundingRect.size.height) * 0.5, self.commentCountLabelLayout.textBoundingRect.size.width, self.commentCountLabelLayout.textBoundingRect.size.height);
    }
    self.bottomLineFrame = CGRectMake(0, kFeedCommentCountViewHeight - 0.5, kScreenWidth, 0.5);
    self.height = kFeedCommentCountViewHeight;
}

@end



